package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.PromotionPosterBO;
import com.junzhiit.nengji.dao.PromotionPosterDAO;
import com.junzhiit.nengji.model.PromotionPoster;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.SystemUserType;

@Repository
public class PromostionPosterDAOImpl extends AbstractBaseDAO<PromotionPoster>implements PromotionPosterDAO {

	@Override
	public List<PromotionPosterBO> getAllList(PromotionPosterBO ppb, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		StatementParameter params = new StatementParameter();
		sql.append("select");
		sql.append(" pp.*");
		sql.append(" from");
		sql.append(" promotion_poster pp");
		sql.append(" where pp.is_delete=? and pp.platform=?");
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(ppb.getPlatform());
		if (ppb.getPlatform() == SystemUserType.SHOP.toIntStatus()) {
			sql.append(" and pp.cs_id=?");
			params.setString(ppb.getCsId());
		}
		if (StringUtils.isNotBlank(ppb.getFileName())) {
			sql.append(" and pp.file_name like ?");
			params.setString("%" + ppb.getFileName() + "%");
		}
		if (ppb.getType() != null) {
			sql.append(" and pp.type=?");
			params.setInt(ppb.getType());
		}
		return super.queryForPagingList(sql.toString(), PromotionPosterBO.class, params, pageNumber, maxPageRow);
	}

	@Override
	public Integer getTotalCount(PromotionPosterBO ppb) {
		StringBuffer sql = new StringBuffer();
		StatementParameter params = new StatementParameter();
		sql.append("select");
		sql.append(" count(*)");
		sql.append(" from");
		sql.append(" promotion_poster pp");
		sql.append(" where pp.is_delete=? and pp.platform=?");
		params.setInt(IsDelete.NO.toIntValue());
		params.setInt(ppb.getPlatform());
		if (ppb.getPlatform() == SystemUserType.SHOP.toIntStatus()) {
			sql.append(" and pp.cs_id=?");
			params.setString(ppb.getCsId());
		}
		if (StringUtils.isNotBlank(ppb.getFileName())) {
			sql.append(" and pp.file_name like ?");
			params.setString("%" + ppb.getFileName() + "%");
		}
		if (ppb.getType() != null) {
			sql.append(" and pp.type=?");
			params.setInt(ppb.getType());
		}
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public PromotionPosterBO findById(PromotionPosterBO ppb) {
		StringBuffer sql = new StringBuffer();
		StatementParameter param = new StatementParameter();
		sql.append("select");
		sql.append(" pp.*");
		sql.append(" from");
		sql.append(" promotion_poster pp");
		sql.append(" where pp.is_delete=? and pp.id=?");
		param.setInt(Is.NO.toIntValue());
		param.setString(ppb.getId());
		return super.query(sql.toString(), PromotionPosterBO.class, param);
	}

	@Override
	public List<PromotionPosterBO> findPageAll(String csId,Integer type, int pageNumber, int maxPageRow) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,file_name,cs_id,type ");
		sql.append(" from promotion_poster");
		sql.append(" where is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(type != null){
			sql.append(" and type =?");
			params.setInt(type);
		}
		if(StringUtils.isNoneBlank(csId)){
			sql.append(" and cs_id =?");
			params.setString(csId);
		}else{
			sql.append(" and cs_id IS NULL");
		}
		
		return super.queryForPagingList(sql.toString(), PromotionPosterBO.class,params,pageNumber,maxPageRow);
	}

	@Override
	public int totalCount(String csId,Integer type) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from promotion_poster");
		sql.append(" where is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(type != null){
			sql.append(" and type =?");
			params.setInt(type);
		}
		if(StringUtils.isNoneBlank(csId)){
			sql.append(" and cs_id =?");
			params.setString(csId);
		}else{
			sql.append(" and cs_id IS NULL");
		}
		
		return super.queryForInt(sql.toString(),params);
	}
}
